<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Componentized Templates</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="tips.html" title="Chapter 21. Tips &amp; Tricks">
<link rel="prev" href="tips.wap.html" title="WAP/WML">
<link rel="next" href="tips.obfuscating.email.html" title="Obfuscating E-mail Addresses">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Componentized Templates</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="tips.wap.html">Prev</a> </td>
<th width="60%" align="center">Chapter 21. Tips &amp; Tricks</th>
<td width="20%" align="right"> <a accesskey="n" href="tips.obfuscating.email.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Componentized Templates">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="tips.componentized.templates"></a>Componentized Templates</h2></div></div></div>
<p>
    Traditionally, programming templates into your applications goes as
    follows: First, you accumulate your variables within your PHP
    application, (maybe with database queries.) Then, you instantiate your
    Smarty object, <a class="link" href="api.assign.html" title="assign()"><code class="varname">assign()</code></a>
    the variables and <a class="link" href="api.display.html" title="display()"><code class="varname">display()</code>
    </a> the template. So lets
    say for example we have a stock ticker on our template. We would
    collect the stock data in our application, then assign these variables
    in the template and display it. Now wouldn't it be nice if you could
    add this stock ticker to any application by merely including the
    template, and not worry about fetching the data up front?
   </p>
<p>
    You can do this by writing a custom plugin for fetching the content and
    assigning it to a template variable.
   </p>
<div class="example">
<a name="id469142"></a><p class="title"><b>Example 21.7. componentized template</b></p>
<div class="example-contents">
<p>
      <code class="filename">function.load_ticker.php</code> -
      drop file in
      <a class="link" href="variable.plugins.dir.html" title="$plugins_dir">
      <em class="parameter"><code>$plugins directory</code></em></a>
    </p>
<pre class="programlisting">

&lt;?php

// setup our function for fetching stock data
function fetch_ticker($symbol)
{
   // put logic here that fetches $ticker_info
   // from some ticker resource
   return $ticker_info;
}

function smarty_function_load_ticker($params, $smarty)
{
   // call the function
   $ticker_info = fetch_ticker($params['symbol']);

   // assign template variable
   $smarty-&gt;assign($params['assign'], $ticker_info);
}
?&gt;

    </pre>
<p>
          <code class="filename">index.tpl</code>
    </p>
<pre class="programlisting">

{load_ticker symbol='SMARTY' assign='ticker'}

Stock Name: {$ticker.name} Stock Price: {$ticker.price}

    </pre>
</div>
</div>
<br class="example-break"><p>
    See also
    <a class="link" href="language.function.include.php.html" title="{include_php}"><code class="varname">{include_php}</code></a>,
    <a class="link" href="language.function.include.html" title="{include}"><code class="varname">{include}</code></a>
    and
    <a class="link" href="language.function.php.html" title="{php}"><code class="varname">{php}</code></a>.
   </p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="tips.wap.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="tips.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="tips.obfuscating.email.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">WAP/WML </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Obfuscating E-mail Addresses</td>
</tr>
</table>
</div>
</body>
</html>
